class CrsForm < ActiveRecord::Base
  belongs_to :phone
  acts_as_mappable :latcolumnname => :latitude, :lngcolumnname => :longitude
  
  def message
    message = self.read_attribute("message")
    if message.nil?
      message = self.read_attribute("content").gsub("msg ","").gsub("Msg ","").gsub("message ","").gsub("Message ","").gsub("locate ","").gsub("Locate ","").gsub("loc ","").gsub("Loc ","")
      self.write_attribute("message",message)
      self.save!
    end
    self.read_attribute("message")
  end

  # def full_address
  #   self.street_number+" "+self.thoroughfare+" "+self.locality+", "+self.state+", "+self.country+" "+self.postalcode
  # end
  # 
  def street_number
    street_number = read_attribute("street_number")
    street_number ||= ""
  end
  # 
  # def thoroughfare
  #   thoroughfare = read_attribute("thoroughfare")
  #   thoroughfare ||= ""
  # end

  def color
    color = :red if self.message.scan("red")
    color = :green if self.message.scan("green")
    color = :blue if self.message.scan("blue")
  end

  def re_geocode
      geo = GeoKit::GeoLoc.geocode(self.address)
      errors.add(:address, "Could not Geocode address") if !geo.success
      # self.lat, self.lng = geo.lat,geo.lng if geo.success
      if geo.success
        self.address = geo.full_address
        self.country = geo.country_code
        self.state = geo.state
        self.locality = geo.city
        self.street_number = geo.street_number unless geo.street_number.nil?
        self.thoroughfare = geo.street_name
        self.postalcode = geo.zip
        self.accuracy = geo.precision

        self.latitude = geo.lat
        self.longitude = geo.lng
        self.ping_type = 0
        self.save!
      end
  end
  
  def diameter
    diam = (100/(Time.now - self.created_at))
    if diam < 0
      0
    else
      diam
    end
  end
  
  
  ### EVERYTHING ABOVE HERE IS THE SAME AS PING.RB - THIS IS ALL JUST FIELDS FOR THE CRS QUESTIONNAIRE

  ### General Information ###

  def crs_type
    case self.g1
    when 1
      "Flood"
    when 2
      "Cyclone"
    when 3
      "Earthquake"
    when 4
      "Landslide"
    when 5
      "Industrial/Chemical"
    when 6
      "Tsunami"
    when 7
      "Fire"
    else
      "Unknown Disaster Type"
    end
  end
  
  def crs_village
    case self.g2
    when 0
      "Village A"
    when 1
      "Village B"
    when 2
      "Village C"
    when 3
      "Village D"
    when 4
      "Village E"
    when 5
      "Village F"
    when 6
      "Village G"
    else
      "Unknown Village"
    end
  end
  
  def crs_panchayat
    case self.g3
    when 1
      "Gram Panchayat A"
    when 2
      "Gram Panchayat B"
    when 3
      "Gram Panchayat C"
    when 4
      "Gram Panchayat D"
    when 5
      "Gram Panchayat E"
    when 6
      "Gram Panchayat F"
    else
      "Unknown Gram Panchayat"
    end
  end

  def crs_block
    case self.g4
    when 1
      "Block A"
    when 2
      "Block B"
    when 3
      "Block C"
    when 4
      "Block D"
    when 5
      "Block E"
    when 6
      "Block F"
    else
      "Unknown Block"
    end
  end
  
  def crs_district
    case self.g5
    when 1
      "District A"
    when 2
      "District B"
    when 3
      "District C"
    when 4
      "District D"
    when 5
      "District E"
    when 6
      "District F"
    else
      "Unknown District"
    end
  end
  
  def crs_population
    self.g6
  end
  
  def crs_total_fam
    self.g7
  end

  def crs_affected
    self.g8
  end
  
  def crs_missing
    self.g9
  end
  
  def crs_deaths
    self.ga
  end
  
  ### Females Affected ###
  
  def crs_f_under_5yrs
    self.d1
  end
  
  def crs_f_5_14yrs
    self.d2
  end
  
  def crs_f_15_59yrs
    self.d3
  end
  
  def crs_f_over_60yrs
    self.d4
  end
  
  def crs_f_preg
    self.d5
  end
  
  def crs_f_unaccompanied
    self.d6
  end
  
  def crs_f_challenged
    self.d7
  end    
  
  def crs_f_hiv
    self.d8
  end
  
  def crs_f_vulnerable
    self.d9
  end
  
  ### Males Affected ###
  
  def crs_m_under_5yrs
    self.da
  end
  
  def crs_m_5_14yrs
    self.db
  end
  
  def crs_m_15_59yrs
    self.dc
  end
  
  def crs_m_over_60yrs
    self.dd
  end
  
  def crs_m_unaccompanied
    self.de
  end
  
  def crs_m_challenged
    self.df
  end    
  
  def crs_m_hiv
    self.dg
  end
  
  def crs_m_vulnerable
    self.dh
  end
  
  ### Health ###
  
  def crs_health_services
    self.h1
  end
  
  def crs_debris_removed
    case self.h2
    when 1
      "Yes"
    when 0
      "No"
    else
      "Unknown"
    end
  end
  
  def crs_morbidity
    morbidity = {}
    morb_data = self.h3
    morbidity['Injuries'] = morb_data.include? 'a'
    morbidity['Diarrheal Disease'] = morb_data.include? 'b'
    morbidity['Measles'] = morb_data.include? 'c'
    morbidity['Malnutrition'] = morb_data.include? 'd'
    morbidity['Dehydration'] = morb_data.include? 'e'
    morbidity['Acute Respiratory Infections'] = morb_data.include? 'f'
    morbidity['Malaria'] = morb_data.include? 'g'
    morbidity['Exposure'] = morb_data.include? 'h'
    morbidity['Other'] = morb_data.include? 'i'
    morbidity
  end
  
  def crs_morb_injuries
    self.h4
  end
  
  def crs_morb_diarrheal
    self.h5
  end
  
  def crs_morb_measles
    self.h6
  end
  
  def crs_morb_malnutrition
    self.h7
  end
  
  def crs_morb_dehydration
    self.h8
  end
  
  def crs_morb_respiratory
    self.h9
  end
  
  def crs_morb_malaria
    self.ha
  end
  
  def crs_morb_exposure
    self.hb
  end
  
  def crs_morb_other
    self.hc
  end
  
  ### INFRASTRUCTURE ###
  
  def crs_services_school
    case self.i1
    when 1
      "Fully Damaged"
    when 2
      "Partially Damaged"
    when 3
      "No Damage"
    end
  end
  
  def crs_services_office
    case self.i2
    when 1
      "Fully Damaged"
    when 2
      "Partially Damaged"
    when 3
      "No Damage"
    end
  end
  
  def crs_services_phc
    case self.i3
    when 1
      "Fully Damaged"
    when 2
      "Partially Damaged"
    when 3
      "No Damage"
    end
  end
  
  def crs_services_available
    services = {}
    serv_data = self.i4
    services['Road out of the affected area for victims'] = morb_data.include? 'a'
    services['Road into the affected area for aid'] = morb_data.include? 'b'
    services['Electricity/Power Supply'] = morb_data.include? 'c'
    services['Telecommunications Systems'] = morb_data.include? 'd'
    services['PDS'] = morb_data.include? 'e'
    services['Adequate Emergency Shelter'] = morb_data.include? 'f'
    services['Space for control room'] = morb_data.include? 'g'
    services['Local partner network'] = morb_data.include? 'h'
    services['Means to manage relief stock'] = morb_data.include? 'i'
    services['Distribution place and volunteers'] = morb_data.include? 'j'
    services
  end
  
  def crs_warehouses
    self.i5
  end
  
  def crs_vehicles
    self.i6
  end
  
  def crs_boats
    self.i7
  end
  
  ### SHELTER / NFI ###
  
  def crs_safe_shelters
    self.s1
  end
  
  def crs_demarcated_site
    case self.s2
    when 1
      "Yes"
    when 0
      "No"
    else
      "Unknown"
    end
  end
  
  def crs_houses_fully_damaged
    self.s3
  end
  
  def crs_houses_partially_damaged
    self.s4
  end
    
  def crs_living_damaged_shelter
    self.s5
  end  
    
  def crs_living_host_families
    self.s6
  end  
  
  def crs_living_emergency
    self.s7
  end
  
  def crs_living_temporary_shelter
    self.s8
  end
  
  def crs_families_left_village
    self.s9
  end
  
  def crs_lost_cooking
    self.sa
  end
  
  def crs_lost_clothes
    self.sb
  end
  
  def crs_lost_cloths
    self.sc
  end
  
  def crs_lost_sheets
    self.sd
  end
  
  def crs_lost_water
    self.se
  end
  
  def crs_lost_soap
    self.sf
  end
  
  ### WATER / SANITATION ###

  def crs_collect_drinking_water
    water = {}
    water_data = self.w1
    water['River'] = water_data.include? 'a'
    water['Tube Wells'] = water_data.include? 'b'
    water['Wells'] = water_data.include? 'c'
    water['Ponds'] = water_data.include? 'd'
    water
  end
  
  def crs_water_other_uses
    self.w2
  end
  
  def crs_treating_drinking_water
    case self.w3
    when 1
      "Yes"
    when 0
      "No"
    else
      "Unknown"
    end
  end    
  
  def crs_water_treatment_methods
    treatment = {}
    treat_data = self.w4
    treatment['Boiling'] = treat_data.include? 'a'
    treatment['Halogen Tablets'] = treat_data.include? 'b'
    treatment['Water Filter'] = treat_data.include? 'c'
    treatment['Filtration with cloth'] = treat_data.include? 'd'
    treatment['Only Sedimentation'] = treat_data.include? 'e'
    treatment
  end
  
  def crs_toilets_available  
    case self.w5
    when 1
      "Yes"
    when 0
      "No"
    else
      "Unknown"
    end
  end
    
  def crs_toilets_good
    self.w6
  end
  
  def crs_toilets_poor
    self.w7
  end
  
  def crs_drainage_condition
    case self.w8
    when 1
      "Good"
    when 2
      "Poor"
    else
      "Unknown"
    end
  end
  
  def crs_designated_defecation  
    case self.w9
    when 1
      "Yes"
    when 0
      "No"
    else
      "Unknown"
    end
  end
  
  def crs_defecation_away
    case self.wa
    when 1
      "Yes"
    when 0
      "No"
    else
      "Unknown"
    end
  end
        
  ### FOOD ###
  
  def crs_food_2weeks
    self.f1
  end
     
  def crs_fuel_2weeks
    self.f2
  end
  
  def crs_fodder_2weeks
    self.f3
  end
  
  ### RESPONSE BY OTHERS ###
  
  def crs_local_food_people
    self.r1
  end
  
  def crs_local_food_days
    self.r2
  end
  
  def crs_local_water_people
    self.r3
  end
  
  def crs_local_water_days
    self.r4
  end
  
  def crs_local_treatment_people
    self.r5
  end
  
  def crs_local_treatment_days
    self.r6
  end
  
  def crs_local_shelter_people
    self.r7
  end
  
  def crs_local_shelter_days
    self.r8
  end
  
  def crs_local_cloth_people
    self.r9
  end
  
  def crs_local_cloth_days
    self.ra
  end
  
  def crs_local_fodder_people
    self.rb
  end
  
  def crs_local_fodder_days
    self.rc
  end
  
  def crs_ngo_food_people
    self.rd
  end
  
  def crs_ngo_food_days
    self.re
  end
  
  def crs_ngo_water_people
    self.rf
  end
  
  def crs_ngo_water_days
    self.rg
  end
  
  def crs_ngo_treatment_people
    self.rh
  end
  
  def crs_ngo_treatment_days
    self.ri
  end
  
  def crs_ngo_shelter_people
    self.rj
  end
  
  def crs_ngo_shelter_days
    self.rk
  end
  
  def crs_ngo_cloth_people
    self.rl
  end
  
  def crs_ngo_cloth_days
    self.rm
  end
  
  def crs_ngo_fodder_people
    self.rn
  end
  
  def crs_ngo_fodder_days
    self.ro
  end
  
  ### PROTECTION ###
    
  def crs_dangers
    dangers = {}
    danger_data = self.p1
    dangers['Sexual abuse / rape'] = danger.data.include? 'a'
    dangers['Abduction / Trafficking'] = danger.data.include? 'b'
    dangers['Forced migration'] = danger.data.include? 'c'
    dangers['Forced militia recruitment'] = danger.data.include? 'd'
    dangers['HIV'] = danger.data.include? 'e'
    dangers['Other concerned diseases'] = danger.data.include? 'f'
    dangers['Any other'] = danger.data.include? 'g'
    dangers
  end
  
  def crs_places_safe  
    case self.p2
    when 1
      "Yes"
    when 0
      "No"
    else
      "Unknown"
    end
  end
   
  def crs_still_attending_school
    self.p3
  end
  
  ### ADDITIONAL QUESTIONS ###
  
  def crs_lost_livelihoods
    self.x1
  end  
end
